<template>
  <div class="loginView">
    <div class="blockDiv">
      <img
        src="../../../public/img/logo/img1721517091251ef0.png"
        class="imgFloat"
      />
      <div class="loginLeft">
        <h1 class="titleH1">欢迎回来</h1>
        <el-form
          label-position="top"
          label-width="80px"
          :model="formLabelAlign"
          :rules="rules"
          ref="loginruleForms"
        >
          <el-form-item label="账号" prop="phone">
            <el-input v-model="formLabelAlign.phone"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="formLabelAlign.password"
              show-password
            ></el-input>
          </el-form-item>
          <div class="lostPassword" @click="goForget">忘记密码</div>
          <el-form-item class="loginButtom">
            <el-button type="primary" @click="submitForm('loginruleForms')"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="blockDiv2">
      <img src="../../../public/img/bgImg/tt.png" />
      <div class="inner">
        <div class="innerText">
          <div class="title">还未注册？</div>
          <div class="subtitle">立即注册,发现新天地！</div>
          <div class="zhuCeBtn" @click="goRegister">注册</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { get_user_login } from "../../api/user_login";
import { set_localUserInfo } from "../../common/localStorageAPI";
export default {
  data() {
    return {
      formLabelAlign: {
        phone: "",
        password: "",
      },
      rules: {
        phone: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      redirect: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  methods: {
    goRegister() {
      this.$router.push("/register");
    },
    goForget() {
      this.$router.push("/forget");
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            let resData = await get_user_login(this.formLabelAlign);
            console.log("登录信息：", resData);
            if (resData.code === 200) {
              // if (resData.data[0].studus == "已删除") {
              //   return this.$alert("该账号当前已被封禁，请联系管理员", "提示", {
              //     confirmButtonText: "确定",
              //     type: "warning",
              //     callback: () => {},
              //   });
              // }
              this.$message({
                showClose: true,
                message: "登录成功",
                type: "success",
              });
              this.$store.commit("SET_USERINFO", resData.data[0]);
              this.$store.commit("SET_TOKEN", resData.token);
              // set_localToken(resData.token);
              set_localUserInfo({
                phone: resData.data[0].phone,
                password: resData.data[0].password,
                date: new Date(),
              });
              this.$router.push({ path: this.redirect || "/home" });
              // this.$router.push("/home");
            } else {
              // this.$message.error(resData.mess);
              this.$alert(resData.mess, "提示", {
                confirmButtonText: "确定",
                type: "warning",
                callback: () => {},
              });
            }
          } catch (error) {
            this.$message.error("网络异常：" + error);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.loginView {
  margin-top: -30px;
  width: 100%;
  height: 560px;
  background: #fff;
  // text-align: center;
  display: flex;
  .blockDiv {
    width: 80%;
    // background: cornflowerblue;
    height: 560px;
    .imgFloat {
      float: left;
      width: 250px;
      margin: 10px;
    }
    .loginLeft {
      .el-form--label-top .el-form-item__label {
        padding: 0;
      }
      .el-form-item {
        margin-bottom: 5px;
      }
      .titleH1 {
        text-align: center;
      }
      width: 30%;
      margin: 0 auto;
      padding-top: 30px;
      .lostPassword {
        text-align: center;
        margin-top: 15px;
        font-size: 14px;
        text-decoration: underline;
        cursor: pointer;
        color: dimgray;
      }
      .loginButtom {
        text-align: center;
        .el-button {
          width: 100%;
          margin-top: 40px;
          background: #1d2f70;
          border: #1d2f70;
          color: #fff;
          margin-left: 5px;
          &:hover {
            background: #7d99e2;
          }
        }
      }
    }
  }
  .blockDiv2 {
    width: 20%;
    height: 560px;
    position: relative;
    img {
      // width: 100%;
      height: 560px;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      max-width: 100%;
      margin: auto;
      z-index: 1;
      opacity: 1;
    }
    .inner {
      color: #fff;
      position: absolute;
      z-index: 2;
      width: 100%;
      .innerText {
        width: 200px;
        text-align: center;
        margin: 0 auto;
        .title {
          margin-top: 90px;
          font-size: 28px;
          font-weight: 500;
          font-weight: 600;
        }
        .subtitle {
          margin-top: 10px;
          font-weight: 600;
        }
        .zhuCeBtn {
          border: 2px solid #fff;
          padding: 10px 10px;
          border-radius: 10px;
          font-size: 20px;
          font-weight: 600;
          margin-top: 220px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
